package com.stylefeng.guns.modular.system.service.impl;

import com.stylefeng.guns.common.enu.BillTypeEnum;
import com.stylefeng.guns.common.persistence.model.Bill;
import com.stylefeng.guns.modular.service.MailService;
import com.stylefeng.guns.modular.service.entity.EmailMessage;
import com.stylefeng.guns.modular.system.dao.BillDao;
import com.stylefeng.guns.modular.system.service.BillService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-05-18
 */
@Service
public class BillServiceImpl implements BillService {

    @Resource
    private BillDao mapper;

    @Resource
    private MailService mailService;

    @Override
    public List<Bill> search(@RequestParam String content, @RequestParam String startTime, @RequestParam String endTime) {
        return mapper.search(content, startTime, endTime);
    }

    @Override
    public Bill getBill(@Param("id") Integer id) {
        return mapper.getBill(id);
    }

    @Override
    public void addBill(Bill bill) {
        mapper.addBill(bill);
        mailService.sendEmail(getEmailMessage(1, bill));
    }

    @Override
    public void executeBill(Bill bill){
        if(bill.getModifyUser() == 1002){
            bill.setState(2);
            mapper.updateState(bill);
            mailService.sendEmail(getEmailMessage(2, bill));
        }
        if(bill.getModifyUser() == 1003){
            bill.setState(3);
            mapper.updateState(bill);
            mailService.sendEmail(getEmailMessage(3, bill));
        }
        if(bill.getModifyUser() == 1001){
            bill.setState(0);
            mapper.updateState(bill);
            mailService.sendEmail(getEmailMessage(4, bill));
        }

    }

    // 根据账单信息转换为邮件信息
    private EmailMessage getEmailMessage(int time, Bill bill){
        EmailMessage message = new EmailMessage();
        message.setTime(time);
        message.setApplyName(bill.getPersonName());
        message.setApplyType(BillTypeEnum.getName(bill.getType()));
        message.setApplyMoney(bill.getMoney().toString());
        message.setApplyEmail(null);
        return message;
    }
}
